<template>
  <!--会签管理-单据申请-付款申请弹窗-表单-->
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row style="margin-bottom: 20px;">
          <a-col :span="24">
            <span style="font-size: 20px;font-weight: bold;">付款申请</span>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="12">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="model.title" placeholder="请输入标题"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="对方名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="other">
              <a-input v-model="model.other" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="12">
            <a-form-model-item label="项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectId">
              <a-select :options="levelOneOptions" placeholder="请选择项目" v-model="model.projectId">
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="银行账号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bankAccount">
              <a-input v-model="model.bankAccount" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="12">
            <a-form-model-item label="开户银行" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bankDeposit">
              <a-input v-model="model.bankDeposit" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="付款项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentProject">
              <a-input v-model="model.paymentProject" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="12">
            <a-form-model-item label="付款金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentMoney">
              <a-input v-model="model.paymentMoney" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contract">
              <a-input v-model="model.contract" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="remark"
              style="margin-bottom: 10px;margin-left: 20px;">
              <textarea v-model="model.remark" cols="100" rows="3" style="width: 93%;"></textarea>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="24">
            <a-form-model-item label="附件资料" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="attachment"
              style="margin:0 60px 10px 20px;">
              <j-upload v-model="model.attachment"></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <!-- <a-button @click="save" type="primary">保存</a-button> -->
  </a-spin>
</template>
<script>
  import {
    httpAction,
    getAction
  } from '@/api/manage'
  import {
    validateDuplicateValue
  } from '@/utils/util'

  export default {
    name: 'WtBrandForm',
    components: {},
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false,
      },

    },
    data() {
      return {
        model: {
          countersignTemplateId: '1',
          title: '',
          other: '',
          projectId: '',
          bankAccount: '',
          bankDeposit: '',
          paymentProject: '',
          paymentMoney: '',
          contract: '',
          remark: '',
          attachment: ''
        },
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 5
          },
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          },
        },
        labelCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 2
          },
        },
        wrapperCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 22
          },
        },
        confirmLoading: false,
        // validatorRules: { paymentMoney: [{required: true,pattern: /^[0-9]{99}$/,message: '请填写金额'}]},
        levelOneOptions: [],

        treeData: [],
        fileList: [],
        url: {
          add: '/countersign/wtCountersignPaymentRequest/add',
          edit: '/wtBrand/edit',
          queryById: '/wtBrand/queryById',
        },
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      },
    },
    created() {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model))
      // console.log('dfsa',this.countersignTemplateId);
    },
    methods: {
      loadTree() {
        var that = this
        getAction(`/sys/sysDepart/getDepartByOrgType?orgType=1`).then((res) => {
          if (res.success) {

            this.treeData = []
            let treeList = res.result
            for (let a = 0; a < treeList.length; a++) {
              let temp = treeList[a]
              temp.isLeaf = temp.leaf
              this.treeData.push(temp)
            }

            this.levelOneOptions = [...this.treeData].map((v) => {
              return {
                ...v,
                value: v.id,
                label: v.departName,
              }
            })
          }
        })
      },
      add(record) {
        //this.edit(this.modelDefault);
        this.model.countersignTemplateId = record.id;
        this.edit();
      },
      edit(record) {
        //this.model = Object.assign({}, record)
        this.visible = true
        this.loadTree()
      },
      save() {
        const that = this
        // 触发表单验证
        this.$refs.form.validate((valid) => {
          if (valid) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            // if (!this.model.id) {
            //   httpurl += this.url.add
            //   method = 'post'
            // } else {
            //   httpurl += this.url.edit
            //   method = 'put'
            // }
            that.model.status = '0'
            // console.log('fde',that.model);
            // debugger
            httpAction('/countersign/wtCountersignPaymentRequest/add', this.model, 'POST')
              .then((res) => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.$emit('ok')
                } else {
                  that.$message.warning(res.message)
                  that.$emit('error')
                }
              })
              .finally(() => {
                that.confirmLoading = false
              })
          }
        })
      },
      submitForm(record) {
        const that = this
        if (!this.model.attachment) {
          that.$message.warning('附件资料不能为空,请上传')
        } else {
          // 触发表单验证
          this.$refs.form.validate((valid) => {
            if (valid) {
              that.confirmLoading = true
              let httpurl = ''
              let method = ''
              that.model.status = record
              // debugger
              httpAction('/countersign/wtCountersignPaymentRequest/add', this.model, 'POST')
                .then((res) => {
                  if (res.success) {
                    that.$message.success(res.message)
                    that.$emit('ok')
                  } else {
                    that.$message.warning(res.message)
                    that.$emit('error')
                  }
                })
                .finally(() => {
                  that.confirmLoading = false
                })
            }
          })
        }
      },
    },
  }
</script>